﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

namespace WSDatos
{
    public class clsDatos
    {
        #region Constructor
       
        public clsDatos()
        {
            
        }
        #endregion
        #region propiedades de la conexion
        private string _servidor;
        private string _bd;
        private string _usuario;
        private string _pass;
        private string _cadenaCnn;
        private Object _Cnn;

        public string Servidor
        {
            get { return _servidor; }
            set { _servidor = value; }
        }
        public string Bd
        {
            get { return _bd; }
            set { _bd = value; }
        }
        public string Usuario
        {
            get { return _usuario; }
            set { _usuario = value; }
        }
        public string Password
        {
            get { return _pass; }
            set { _pass = value; }
        }
        public string Cadena
        {
            get { return _cadenaCnn; }
            set { _cadenaCnn = value; }
        }
        public Object Conexion
        {
            get { _Cnn = FnAbreCnn();
            return _Cnn;
            }
        
        }

        public string FnCadenaCnn()
        {
            string cadCnn = "";
            try
            {
                if (Servidor != "" && Bd != "" && Password != "" && Usuario != "")
                    cadCnn = "Server=localhost;Database=crm;User ID=root;Password=root;Pooling=false";                        
            }
            catch (Exception varEx)
            {
                if (varEx.Message != "") { }
            }
            return cadCnn;
        }

        Object FnAbreCnn()
        {
            object cnn = null;
            try
            {
                MySqlConnection cnn01 = new MySqlConnection();                
                cnn01.ConnectionString = FnCadenaCnn();
                cnn01.Open();
                cnn = cnn01;                
            }
            catch (Exception varEx)
            {
                if (varEx.Message != "") { }
                    cnn = null;
            }
            return cnn;
        }

        #endregion
        #region Lectura de tablas
        public DataTable FncTabla (string commando, string nombre, object cnn)
        {
            DataTable dtt= null;
            try
            {           
                MySqlConnection cnn01 = (MySqlConnection)cnn;
                MySqlDataAdapter da = new MySqlDataAdapter(commando, cnn01);
                dtt = new DataTable();
                da.Fill(dtt);
                dtt.TableName = nombre;                        
            }
            catch (Exception varEx)
            {
            if(varEx.Message !=""){}
                dtt = null;
            }
            return dtt;
        
        }
        public DataTable FncTabla(object commando, string nombre)
        {
            
            DataTable dtt = null;
            try
            {
                MySqlCommand cmd01 = (MySqlCommand)commando;
                MySqlDataAdapter da = new MySqlDataAdapter(cmd01);
                dtt = new DataTable();
                da.Fill(dtt);
                dtt.TableName = nombre;                
            }
            catch (Exception varEx)
            {
                if (varEx.Message != "") { }
                dtt = null;
            }
            return dtt;

        }
        public int FncComando(object commando)
        {
            int dato = -1;
            try
            {
                MySqlCommand cmd01 = (MySqlCommand)commando;
                cmd01.Connection = (MySqlConnection)Conexion;
                dato = cmd01.ExecuteNonQuery();             
            }
            catch (Exception varEx)
            {
                if (varEx.Message != "") { }
                    dato = -1;
            }
            return dato;
        }

        #endregion
    }
}
